define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/coupon/index',
                    add_url: 'coupon/coupon/add',
                    edit_url: '',
                    del_url: 'coupon/coupon/del',
                    multi_url: '',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'coupon.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'title', title: __('Title')},
                        {field: 'prefix', title: __('Title prefix'), operate: 'LIKE'},
                        {field: 'coupon_info', title: __('Coupon info'), operate: 'LIKE',searchable:false},
                        {field: 'nums', title: __('Nums'), operate: 'LIKE',searchable:false},
                        {field: 'deadlinemode', title: __('DeadlineMode'), operate: 'LIKE',searchable:false},
                        {field: 'channel', title: __('Channel'), operate: 'LIKE',searchable:false},

                        {
                            field: 'couponlist',
                            title: __('Couponlist'),
                            table: table,
                            operate: false,
                            buttons: [{
                                name: 'couponlist',
                                text: __('Couponlist info'),
                                title: __('Couponlist info'),
                                classname: 'btn btn-xs btn-danger btn-dialog',
                                icon: 'fa fa-magic',
                                url: 'coupon/coupon/couponlist',
                                visible: function(row) {
                                    //返回true时按钮显示,返回false隐藏
                                    return true;
                                }
                            }, ],
                            formatter: Table.api.formatter.buttons
                        },

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        couponlist: function() { },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});